package hiatao.puzzle.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;

import haitao.puzzle.R;
import hiatao.puzzle.adapter.PuzzleListAdapter;
import hiatao.puzzle.config.MyApplication;
import hiatao.puzzle.config.URLS;
import hiatao.puzzle.entity.PuzzleInfo;
import hiatao.puzzle.entity.User;
import hiatao.puzzle.utils.ImageUtils;
import hiatao.puzzle.utils.PixelUtil;
import hiatao.puzzle.utils.StatusBarCompat;
import hiatao.puzzle.utils.Tools;
import hiatao.puzzle.widget.PullToZoomListView;
import hiatao.puzzle.widget.PullToZoomListView.PullToZoomListViewListener;


public class UserActivity extends Activity implements PullToZoomListViewListener ,OnClickListener{

	int i = 0;
	private PullToZoomListView mListView;
	private PuzzleListAdapter mAdapter;
	private View top_back;
	private View btn_more;
	//private TextView sixin_tv;
	private TextView title_tv;
	private TextView intro_tv;
	private View follow_tv;
	private View sixin_tv;
	private TextView myfollow_tv;
	private TextView fans_tv;
	private TextView zuan_tv;
	private TextView original_tv;
	private TextView like_tv;
	private ImageView head_img;
	private LinearLayout ly1;
	private LinearLayout ly2;
	private LinearLayout ly3;
	private View ly1_underline;
	private View ly2_underline;
	private View ly3_underline;
	

	private LinearLayout layout[];
	int index = 0;
	private boolean self;
	private User user;
	private View pop_view;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user);
		initListView();
		StatusBarCompat.compat(this, getResources().getColor(R.color.primary));
	}

	 

	 private void initListView() {
		 
		 self = getIntent().getBooleanExtra("SELF", true);
		 user = (User) getIntent().getSerializableExtra("USER");
		 
		 if(!self){
			
		 }else{
			 user= MyApplication.getInstance().user;
		 }
		mListView=(PullToZoomListView) findViewById(R.id.listView01);
		mAdapter = new PuzzleListAdapter(user,UserActivity.this);

		/*========================= 设置头部的图片====================================*/
		mListView.getHeaderView().setScaleType(ImageView.ScaleType.CENTER_CROP);
		mListView.getHeaderView().setImageResource(R.drawable.story_member_center_bg);
		
		/*========================= 设置头部的高度 ====================================*/
		mListView.setmHeaderHeight(PixelUtil.dp2px(UserActivity.this, 300));

		/*========================= 设置头部的的布局====================================*/
		 View mHeaderView=getLayoutInflater().inflate(R.layout.layout_story_userinfo, null);
		 mListView.getHeaderContainer().addView(mHeaderView);
		 mListView.setHeaderView();
		 mListView.setAdapter(mAdapter);
		 mListView.setPullToZoomListViewListener(this);
		 changeData();
		 
		 
		 top_back =  findViewById(R.id.top_back);
		 btn_more = findViewById(R.id.btn_more);
		 title_tv = (TextView) findViewById(R.id.title);
		 
		 
		 head_img = (ImageView) mHeaderView.findViewById(R.id.head_image);
		 intro_tv = (TextView) mHeaderView.findViewById(R.id.intro_tv);
		 zuan_tv = (TextView)  mHeaderView.findViewById(R.id.zuan_tv);
		 myfollow_tv = (TextView) mHeaderView.findViewById(R.id.myfollow_tv);
		 fans_tv = (TextView) mHeaderView.findViewById(R.id.fans_tv);
		 original_tv = (TextView) mHeaderView.findViewById(R.id.original_tv);
		 like_tv = (TextView) mHeaderView.findViewById(R.id.like_tv);
		 
		 ly1 =  (LinearLayout) mHeaderView.findViewById(R.id.ly1);
		 ly2 =  (LinearLayout) mHeaderView.findViewById(R.id.ly2);
		 ly3 =  (LinearLayout) mHeaderView.findViewById(R.id.ly3);
		 ly1_underline=findViewById(R.id.ly1_underline);
		 ly2_underline=findViewById(R.id.ly2_underline);
		 ly3_underline=findViewById(R.id.ly3_underline);
		 layout = new LinearLayout[]{ly1,ly2,ly3};
		 ly1.setOnClickListener(this);
		 ly2.setOnClickListener(this);
		 ly3.setOnClickListener(this);
		 myfollow_tv.setText("关注 "+Tools.getRandom(8, 300));
		 fans_tv.setText("粉丝 "+Tools.getRandom(8, 300));
		 original_tv.setText(""+Tools.getRandom(2, 60));
		 like_tv.setText(""+Tools.getRandom(1, 100));
		 

		 if(!self){
			 btn_more.setVisibility(View.VISIBLE);
			 title_tv.setText(user.getName());
			 intro_tv.setText("简介："+user.getIntro());
			 zuan_tv.setText(""+user.getZuanNum());
			 ImageUtils.loadImg(user.getHeadUrl(), head_img);
			 
		 }else{
			 title_tv.setText(MyApplication.getInstance().user.getName());
			 ImageUtils.loadImg(MyApplication.getInstance().user.getHeadUrl(), head_img);
		 }
		 
		 top_back.setOnClickListener(new OnClickListener() {
				
			@Override
			public void onClick(View v) {
				 UserActivity.this.finish();
			}
		});
		 myfollow_tv.setOnClickListener(new OnClickListener() {
			 
			 @Override
			 public void onClick(View v) {
				 
				 String title = "关注";
				 if(!self){
					 title=user.getName()+"的关注";
				 }
				 Tools.goActivity(UserActivity.this, FollowActivity.class, new String[]{"TITLE"}, new String[]{title});
			 }
		 });
		 fans_tv.setOnClickListener(new OnClickListener() {
			 
			 @Override
			 public void onClick(View v) {
				 String title = "粉丝";
				 if(!self){
					 title=user.getName()+"的粉丝";
				 }
				 Tools.goActivity(UserActivity.this, FollowActivity.class, new String[]{"TITLE"}, new String[]{title});
			 }
		 });
		 
		 
		 
		 
		 //
		 pop_view = getLayoutInflater().inflate(R.layout.share_popup_window, null);
		 btn_more.setOnClickListener(new OnClickListener() {
			 @Override
			 public void onClick(View v) {
				 showPpw();
			 }
			 
		 });
		 
	}
	

	@Override
	public void onLoadMore() {
		
  }

	@Override
	public void onClick(View v) {
		
		int x =0;
		switch (v.getId()) {
		case R.id.ly1:
			x=0;
			ly1_underline.setVisibility(View.VISIBLE);
			ly2_underline.setVisibility(View.GONE);
			ly3_underline.setVisibility(View.GONE);
			break;
		case R.id.ly2:
			x=1;
			ly1_underline.setVisibility(View.GONE);
			ly2_underline.setVisibility(View.VISIBLE);
			ly3_underline.setVisibility(View.GONE);
			break;
		case R.id.ly3:
			x=2;
			ly1_underline.setVisibility(View.GONE);
			ly2_underline.setVisibility(View.GONE);
			ly3_underline.setVisibility(View.VISIBLE);
			break;

		default:
			break;
		}
		if(index !=x){
			/*for(int i =0;i<layout.length;i++){
				
			}*/
		TextView t0=(TextView) layout[x].getChildAt(0);
		TextView t1=(TextView) layout[x].getChildAt(1);
		t0.setTextColor(getResources().getColor(R.color.head_color));
		t1.setTextColor(getResources().getColor(R.color.head_color));
		t0=(TextView) layout[index].getChildAt(0);
		t1=(TextView) layout[index].getChildAt(1);
		t0.setTextColor(getResources().getColor(R.color.tab_nor));
		t1.setTextColor(getResources().getColor(R.color.tab_nor));
		index =x;
		changeData();
		}
	}
	
	private void showPpw(){
		
		final PopupWindow popupWindow = new PopupWindow(pop_view,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);
		popupWindow.setTouchable(true);
		popupWindow.setOutsideTouchable(true);
		// 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
		// 我觉得这里是API的一个bug
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		// 设置好参数之后再show
		popupWindow.showAsDropDown(btn_more, 0, 0);
		 follow_tv = pop_view.findViewById(R.id.follow_view);
		 sixin_tv = pop_view.findViewById(R.id.sixin_view);
		 follow_tv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!MyApplication.getInstance().isLogin) {
					Tools.showToast("请先登陆!");
				} else {
					Tools.showToast("已关注");
					//follow_tv.setText("已关注");
					//follow_tv.setBackgroundResource(R.drawable.btn_follow_press);
				}
				popupWindow.dismiss();
			}
			
		});
		 sixin_tv.setOnClickListener(new OnClickListener() {
			 @Override
			 public void onClick(View v) {
				 // TODO Auto-generated method stub
				 if (!MyApplication.getInstance().isLogin) {
					 Tools.showToast("请先登陆!");
				 } else {
					Intent intent = new Intent(UserActivity.this, SixinActivity.class);
					intent.putExtra("USER", user);
					UserActivity.this.startActivity(intent);
				 }
				 popupWindow.dismiss();
			 }
			 
		 });
	}
	private void changeData() {

		ArrayList<PuzzleInfo> list = new ArrayList<PuzzleInfo>();
		for (int i = 1; i < 5; i++) {
			PuzzleInfo info = new PuzzleInfo();
			info.setPicture(URLS.BASE_PICTURE + ((index) * 10 + 1 + i) + ".jpg");
			info.setPicture2(URLS.BASE_PICTURE + ((index) * 10 + 6 + i) + ".jpg");
			info.setJoinNum(Tools.getRandom(0, 100));
			info.setLikeNum(Tools.getRandom(0, 100));
			list.add(info);
		}
		mListView.setLoadFinish(true);
		mAdapter.clear();
		mAdapter.addItemLast(list);
	}
 
}
